// $_FILEHEADER_BEGIN ***************************
// 版权声明:Timer
// Copyright © 2015 - Timer All Rights Reserved
// 文件名称: AssetsRecordVC.m
// 创建日期: 15/12/13
// 创 建 人: 甄鑫
// 文件说明: <#content#>
// $_FILEHEADER_END ******************************


#import "AssetsRecordVC.h"
#import "UIView+UIView_ITAdditions.h"
#import "ARCellHelper.h"
#import "ARViewHepler.h"
#import "ARDetailView.h"
#import "ARRequestModel.h"
#import "BarrageView.h"
#import "NavHelper.h"


@interface AssetsRecordVC ()
<
    ARBottomViewDelegate,
    AROtherBottomVDelegate,
    UIScrollViewDelegate,
    UITableViewDelegate,
    UITableViewDataSource,
    ARDetailViewDelegate
>
{
    UIImageView *_statuImage;
    UILabel *numlabel;
    NSString *_userID;
}
@property (weak, nonatomic) IBOutlet UIView *mpContentView;
@property (weak, nonatomic) IBOutlet UITableView *mpMainTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainTableViewHeight;
@property (weak, nonatomic) IBOutlet ARDetailView *mpDetailView;
@property (strong, nonatomic) NSMutableArray * dataArr;

@end
#define contentHeight (SCREEN_HEIGHT - 64 - 44)

@implementation AssetsRecordVC
{

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_WHITE];
    [self.navBar setTitle:[ARViewHepler getTitleWithType:self.vcType]];
    [self.navBar setNavBackColor:[ARViewHepler getNavColor:self.vcType]];
    self.view.backgroundColor = [ARViewHepler getNavColor:self.vcType];
    [self.view addSubview:self.mpContentView];
    [self.mpContentView setFrame_y:NAV_BAR_HEIGHT];
    [self.mpContentView setFrame_width:SCREEN_WIDTH];
    
    [self.mpContentView setFrame_height:SCREEN_HEIGHT*2];
    
    [self.mainTableViewHeight setConstant:contentHeight];
    
    UIView * view = [ARBottomView loadBundleNib];
    [view setFrame:CGRectMake(0, SCREEN_HEIGHT - view.size.height, SCREEN_WIDTH, view.size.height)];
    [self.view addSubview:view];
    
    [self.view bringSubviewToFront:self.navBar];
    
    [self.mpDetailView setDelegate:self];
    self.mpDetailView.pageType = self.vcType;
    self.mpDetailView.baseType = self.baseType;
    [self uiConfig];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [ARRequestModel sharedInstance].pageType = self.vcType;
    [ARRequestModel sharedInstance].baseType = self.baseType;
    [ARRequestModel sharedInstance].hopeId = self.hopeId;
    
    [[ARRequestModel sharedInstance] fetchBaseInfoData:^(id output) {
        for (ARCellModel * cellModelItem in self.dataArr) {
            cellModelItem.data = output;
            [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@",output[@"income"]] forKey:@"income"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        [self.mpBottomView setDataWithDict:output];
        [self.mpMainTableView reloadData];
        [[ARRequestModel sharedInstance] fetchBorrowOutAndCB:^(id output) {
            [_mpDetailView setCellData:output withType:Detail_ChuJie];
        }];
        [[ARRequestModel sharedInstance] fetchRepayPlanAndCB:^(id output) {
            [_mpDetailView setCellData:output withType:Detail_HuanKuan];
        }];
        
        if (self.baseType == 1) {
            [[ARRequestModel sharedInstance] fetchHuiKuanAndCB:^(id output) {
                [_mpDetailView setCellData:output withType:Detail_HuiKuan];
            }];
        }
        [[ARRequestModel sharedInstance] fetchBorrowInfoAndCB:^(id output) {
            [_mpDetailView setCellData:output withType:Detail_JieKuan];
        }];

    }];
    
        [self sendMsgSuccess];
    _barrageView.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - **************** DetailViewAnimation
- (void)ARDetailViewShowUpView
{
    [UIView animateWithDuration:0.2 animations:^{
        [self.mpContentView setFrame_y:NAV_BAR_HEIGHT];
    }];
}
#pragma mark - **************** UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.mpMainTableView) {
        if (self.mpMainTableView.contentOffset.y > 80) {
            [UIView animateWithDuration:0.2 animations:^{
                [self.mpContentView setFrame_y:-contentHeight];
            }];
        }
    }
    if (scrollView == self.mpMainTableView) {
        if (scrollView.contentOffset.y<0) {
            self.view.backgroundColor = [ARViewHepler getNavColor:self.vcType];
        }else{
            self.view.backgroundColor = [UIColor whiteColor];
        }
    }
}

#pragma mark - **************** RecvData
- (void)uiConfig
{
    NSArray * cellTypeArr = @[@(AR_CELL_TYPE_TITLE),
                              @(AR_CELL_TYPE_FRIEDN),
                              @(AR_CELL_TYPE_REASON),
                              @(AR_CELL_TYPE_SIMPLE),];
    if (self.baseType == TYPE_BORROW_IN) {
        cellTypeArr = @[@(AR_CELL_TYPE_TITLE),
                        @(AR_CELL_TYPE_REASON),
                        @(AR_CELL_TYPE_SIMPLE),];
    }
    for (NSNumber * typeNumber in cellTypeArr) {
        ARCellModel * cellModel = [[ARCellModel alloc]init];
        cellModel.cellBaseType = [NSString stringWithFormat:@"%ld",self.baseType];
        cellModel.cellType = typeNumber.integerValue;
        cellModel.parentVCType = self.vcType;
        
        [self addARCellModel:cellModel];
    }
    [self.mpMainTableView reloadData];
    
    [self bottomViewHidden];
    [self addRightButton];
    
}
#pragma mark - 底下输入框
- (void)mpBottomCreate{
    self.mpBottomView = [ARBottomView loadBundleNib];
    self.mpBottomView.frame = CGRectMake(0, SCREEN_HEIGHT-45, 320, 44);
    self.mpBottomView.delegate = self;
    self.mpBottomView.baseType = self.baseType;
    self.mpBottomView.pageType = self.vcType;
    self.mpBottomView.hopeID = self.hopeId;
    [self.view addSubview:self.mpBottomView];
}
- (void)otherBottomCreate{
    self.otherBottomView = [AROtherBottomV loadBundleNib];
    self.otherBottomView.frame = CGRectMake(0, SCREEN_HEIGHT-45, 320, 44);
    self.otherBottomView.delegate = self;
    self.otherBottomView.hopeID = self.hopeId;
    [self.view addSubview:self.otherBottomView];
}
- (void)bottomViewHidden{
    switch (self.baseType) {
        case 0://借入
        {
            switch (self.vcType) {
                case 1:case 0:case 11:case 12:
                case 8:case 10:
                {
                    [self otherBottomCreate];
                }
                    break;
                case 2:case 700:
                {
                    [self mpBottomCreate];
                    [self.mpBottomView.borrowBtn setTitle:@"待审核放款" forState:UIControlStateNormal];
                }
                    break;
                case 7:case 6:
                {
                    [self mpBottomCreate];
                    [self.mpBottomView.borrowBtn setTitle:@"重新借款" forState:UIControlStateNormal];
                }
                    break;
                case 5:
                {
                    [self mpBottomCreate];
                    [self.mpBottomView.borrowBtn setTitle:@"立即还款" forState:UIControlStateNormal];
                }
                    break;
                case 4:case 3:case 9:case 13:
                {
                    [self mpBottomCreate];
                    self.mpBottomView.borrowBtn.enabled = NO;
                    [self.mpBottomView.borrowBtn setTitle:@"" forState:UIControlStateNormal];
                }
                default:
                    break;
            }
        }
            break;
        case 1://借出
        {
            switch (self.vcType) {
                case 1:case 0:case 11:case 12:
                {
                    [self mpBottomCreate];
                    [self.mpBottomView.borrowBtn setTitle:@"立即出借" forState:UIControlStateNormal];
                }
                    break;
                case 5:
                {
                    [self otherBottomCreate];
                }
                    break;
                case 8:case 10:
                case 4:case 3:case 9:case 13:
                case 7:case 6:
                case 2:case 700:
                {
                    [self otherBottomCreate];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 2://理财
        {
            if (self.vcType == 1) {
                [self mpBottomCreate];
            }else{
                [self otherBottomCreate];
            }
            
        }
            break;
        default:
            break;
    }
}
#pragma mark - 右上角弹幕开关
- (void)addRightButton{
    UIView *rightBackV = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 100, 44)];
    rightBackV.backgroundColor = [UIColor clearColor];
    
    _statuImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 12, 20, 20)];
    _statuImage.image = [UIImage imageNamed:@"danmuOpen"];
    _statuImage.contentMode = UIViewContentModeScaleToFill;
    [rightBackV addSubview:_statuImage];
    numlabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 13, 55, 18)];
    numlabel.backgroundColor = ColorDefaultOrange;
    numlabel.layer.cornerRadius = 4;
    numlabel.clipsToBounds = YES;
    numlabel.textColor = [UIColor whiteColor];
    numlabel.textAlignment = NSTextAlignmentCenter;
    numlabel.font = [UIFont systemFontOfSize:10];
    numlabel.text = @"37弹幕";
    [rightBackV addSubview:numlabel];
    
    UIButton *danmuBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,_statuImage.frame.size.width , rightBackV.frame.size.height)];
    [danmuBtn addTarget:self action:@selector(danmuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    danmuBtn.selected = YES;
    [rightBackV addSubview:danmuBtn];
    [self.navBar setRightView:rightBackV];
    
    UIButton *msgbtn = [[UIButton alloc] initWithFrame:CGRectMake(numlabel.frame.origin.x, 0,numlabel.frame.size.width , rightBackV.frame.size.height)];
    [msgbtn addTarget:self action:@selector(gotoMsgView:) forControlEvents:UIControlEventTouchUpInside];
    [rightBackV addSubview:msgbtn];
    [self.navBar setRightView:rightBackV];
}
- (void)danmuBtnClick:(UIButton *)sender{
    if (sender.selected == NO) {
        sender.selected = YES;
        _statuImage.image = [UIImage imageNamed:@"danmuOpen"];_barrageView.hidden = NO;
    }else{
        _statuImage.image = [UIImage imageNamed:@"danmuClose"];
        sender.selected = NO;
        _barrageView.hidden = YES;
    }
}
- (void)gotoMsgView:(UIButton *)sender{
    [NavHelper gotoSendMsgVCWithHopeID:self.hopeId];

}
#pragma mark - **************** UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ARCellModel * model = [self getCellModelWithIndex:indexPath.row];
    return [model getCellHeight];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.baseType == TYPE_BORROW_IN) {
        
    }else{
    
    }
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ARCellModel * model = [self getCellModelWithIndex:indexPath.row];
    
    UITableViewCell<ARCellProtocol> * cell = [tableView dequeueReusableCellWithIdentifier:[model getCellIdStr]];
    if (!cell)
    {
        cell = [model getCellNib];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell reSet];
    [cell setData:model];
    [cell setDelegate:self];
    return cell;
}

#pragma mark - **************** ARCellDelegate
- (void)attendantOrderCellClick:(UITableViewCell<ARCellProtocol> *)cell
{
    ARCellModel * model = [cell getData];
    // ....
}
#pragma mark - **************** getter & setter
- (ARCellModel *)getCellModelWithIndex:(NSInteger)index
{
    if (index > self.dataArr.count)
    {
        return nil;
    }
    ARCellModel * model = self.dataArr[index];
    return model;
}

- (void)addARCellModel:(ARCellModel *)model
{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    [self.dataArr addObject:model];
}


#pragma mark - 还款

- (void)startPay{
    
}

#pragma mark - 弹幕
static BarrageView * _barrageView;
- (void)createBarrageWithDataArray:(NSArray *)dataArr{
    if (dataArr.count == 0) {
        return;
    }
    if (!_barrageView) {
        [_barrageView removeFromSuperview];
        _barrageView = nil;
    }
    _barrageView = [[BarrageView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-300, SCREEN_WIDTH, 200)];
    [self.view addSubview:_barrageView];
    NSMutableArray *barrageArr = [[NSMutableArray alloc]init];
    for (NSInteger index = 0;  index<dataArr.count; index ++) {
        NSDictionary *subDict = dataArr[index];
        BarrageItemModel * itemModel = [[BarrageItemModel alloc]init];
        itemModel.uid = subDict[@"friendid"];
        [itemModel setContentStr:subDict[@"content"]];
        [itemModel setPlayTime:(arc4random() % 4 + 4)];
        
        NSInteger type = [[NSString stringWithFormat:@"%@",subDict[@"type"]] integerValue];
        if (type==3) {
            [itemModel setIsShowRedSpecial:1];
        }else{
            [itemModel setIsShowRedSpecial:0];
        }
        NSInteger level = [[NSString stringWithFormat:@"%@",subDict[@"level"]] integerValue];
        if (level==1) {
            [itemModel setRedSpecialContent:[NSString stringWithFormat:@"我的朋友：出借%@",subDict[@"amount"]]];
        }else{
            [itemModel setRedSpecialContent:[NSString stringWithFormat:@"朋友的朋友：出借%@",subDict[@"amount"]]];
        }
        [barrageArr addObject:itemModel];
    }
    [_barrageView setPlayTime:10];
    [_barrageView setBrrageDataArr:barrageArr];
    [_barrageView playAnimation];
}

- (void)sendMsgSuccess{
    [[ARRequestModel sharedInstance] fetchPingLunListAndCB:^(id output) {
        _userID = output[@"userid"];
        [self createBarrageWithDataArray:[output objectForKey:@"data"]];
        numlabel.text = [NSString stringWithFormat:@"%@弹幕",[output objectForKey:@"totalRecords"]];
    }];
}

@end
